package controller.teacher.manager;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import view.DataList;

import dao.Student;

@WebServlet("/teacher/manager/edit")
public class edit extends Base {
    private static final long serialVersionUID = 1L;

    public edit() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            Student.Data d = Student.findById(new Integer(request
                    .getParameter("id")));
            view.displayMap("teacher/edit.jsp", "data", d);
        } catch (NumberFormatException | SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            String id = request.getParameter("id");
            String no = request.getParameter("no");
            String name = request.getParameter("name");
            String pwd = request.getParameter("pwd");
            Integer _class = new Integer(request.getParameter("class"));
            PreparedStatement stmt = Student.update("`id`=" + id, "no", "name", "class");
            stmt.setString(1, no);
            stmt.setString(2, name);
            stmt.setInt(3, _class);
            stmt.executeUpdate();
            view.sendRedirect(request.getContextPath() + "/teacher/manager/index");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
